<?php
namespace app\post\controller\admin;
use app\common\controller\Admin;
use app\common\traits\ControllerCurd;
use think\Request;
use app\common\annotation\NodeAnnotation;
/**
 * @NodeAnnotation(
 *     title="文章分类",
 *     parent="post/admin.index",
 *     is_menu=0,
 *     is_view=0,
 *     auth=1
 * )
 */
class Category extends Admin
{
    use ControllerCurd;
    public $model;
    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\post\model\Category();
    }

    /**
     * @NodeAnnotation(
     *     title="分类管理",
     *     parent="post/admin.category",
     *     MenuRoot="post/admin.index"
     * )
     */
    public function index(Request $request)
    {
        $keywords  = $request->param("keywords");

        $where = [];

        if($keywords) array_push($where,['name',"like","%$keywords%"]);

        $categorList = $this->model->where($where)->paginate();

        $this->assign([
            "categoryList" => $categorList,
            "search"       => [
                "keywords" => $keywords
            ]
        ]);
        return view();
    }

    /**
     * @NodeAnnotation(
     *     title="添加分类",
     *     parent="post/admin.category",
     *     MenuRoot="post/admin.index",
     *     is_menu = 0
     * )
     */
    public function addcategory(Request $request)
    {
        if($request->isAjax())
        {
            $tree = $request->param("dataType");
            if($tree=='tree')
            {
                $data = $this->model->select();
                return json($data);
            }
        }
        $this->add(url('post/admin.category/index'));

        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="编辑分类",
     *     parent="post/admin.category",
     *     MenuRoot="post/admin.index",
     *     is_menu = 0
     * )
     */
    public function editcategory(Request $request,$id=0)
    {
        $category = $this->model->findData($id);
        $this->assign([
            "category" => $category,
            "level"    => $this->model->find($category['parent_id'])
        ]);
        $this->edit(url('post/admin.category/index'));
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="删除分类",
     *     parent="post/admin.category",
     *     MenuRoot="post/admin.index",
     *     is_menu = 0
     * )
     */
    public function delcategory()
    {
        $this->del();
    }
}